package com.onion.orm.support.filter;


import com.onion.utils.surface.PageVo;

import java.util.Map;

public abstract class QueryFilter implements java.io.Serializable {

    private static final long serialVersionUID = 4227366987546055388L;

    protected int offset = 0;
    protected int limit = PageVo.DEFAULT_PAGE_SIZE;
    protected int IN_MAX_SIZE = 1_000;
    protected Map<String, String> fieldMappings;

    public abstract QueryFilter addFilter(String paramName, Object paramValue);

    /**
     * 以页码方式来设置分页
     * @param page 页码
     * @param pageSize 每页大小
     * @return
     */
    public QueryFilter page(int page, int pageSize) {
        if (page < 1) {
            page = 1;
        }
        if (pageSize < 0) {
            pageSize = 1;
        }
        if (pageSize > 10000) {
            throw new RuntimeException("page size is more than 10000?");
        }
        offset = (page - 1) * pageSize;
        limit = pageSize;
        return this;
    }

    /**
     * 设置最多返回的行数
     * @param count
     * @return
     */
    public QueryFilter limit(int count) {
        this.offset = 0;
        this.limit = count;
        return this;
    }

    /**
     * 直接设置sql:
     * limit ${offset}, ${count}
     * @param offset
     * @param count
     * @return
     */
    public QueryFilter limit(int offset, int count) {
        this.offset = offset;
        this.limit = count;
        return this;
    }

    /**
     * 设置排序，注意，这里是有先后顺序的，能按多列来排序
     * @param orderBy
     * @param isDesc
     * @return
     */
    public abstract QueryFilter orderBy(String orderBy, boolean isDesc);

    /**
     * 获取查询条件数据以传递给service
     * @return
     */
    public abstract QueryData getQueryData();

}